<?php

/*
 * Created on Feb 11, 2010
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
?>
<?php

require_once APPPATH . "models/model_dao.php";
class Model_dao extends Model {

	var $_tbl_name_orginal;
	var $_tbl_name;
	var $_tbl_pri;

	function Model_dao($tbl_name, $tbl_pri = "id") {
		$this->_tbl_pri = $tbl_pri;
		$this->_tbl_name_orginal = $tbl_name;
		$this->_tbl_name = $this->_tbl_name_orginal;
		$CI = & get_instance();
		$lang_suffix = $CI->config->item("lang_suffix_default");
		$language_current = $_SESSION[LANGUAGE_CURRENT];
		if (strtolower($lang_suffix) != strtolower($language_current)) {
			$this->_tbl_name .= $CI->config->item('lang_seperator') . strtolower($language_current);
		}
	}

	/**
	 *
	 *
	 */
	function set_table_name($lang_code) {
		if($lang_code!='')
			$this->_tbl_name = $this->_tbl_name_orginal . "_" . $lang_code;
		else
			$this->_tbl_name = $this->_tbl_name_orginal;
	}

	/**
	 *
	 *
	 */
	function clone_table($suffix, $copy_content) {
		$to_table = $this->_tbl_name_orginal . $suffix;
		if ($this->db->table_exists($to_table)) {			
			//$this->db->drop_table($to_table);
		}
		else{
		$sql = "CREATE TABLE {$to_table} AS SELECT * FROM {$this->_tbl_name_orginal}";
		$this->db->query($sql);
		}
	}

	/**
	 *
	 *
	 */
	function clone_record() {

	}
	/**
	 *
	 *
	 */
	function insert($data) {
		$this->db->insert($this->_tbl_name_orginal, $data);
		$insert_id = $this->db->insert_id();
		$data = $this->find_orginal($insert_id);
		// Get language support
		$lang_supports = $_SESSION[LANGUAGE_SUPPORT];
		$lang_suffix = $this->config->item("lang_suffix_default");
		foreach($lang_supports as $item){
			if(strtolower($item['iso_code_2']) != strtolower($lang_suffix)){
				$this->set_table_name($item['iso_code_2']);
				$this->db->insert($this->_tbl_name, $data);
			}
		}
		return $insert_id;
	}
	
	function update($data){
		$this->db->update($this->_tbl_name, $data);
	}
	
	/**
	 *
	 *
	 */
	function find_orginal($id){
		if (intval($id)) {
			$query = $this->db->get_where($this->_tbl_name_orginal, array ($this->_tbl_pri => $id));

			if ($query->num_rows() > 0) {
				return $query->first_row('array');
			}
		}
		return null;
	}
	
	
}
?>